﻿/// <summary>
/// Copyright(c) 2008-2009北京玖富时代投资顾问有限公司, All Rights Reserved.
/// 
/// 通用公共方法--Word操作类。
/// 
/// 版本：Ver 1.0
/// 作者：【周燕龙（借用XXX）】
/// 时间：2009-09-08
/// 
/// 版本：Ver 1.1
/// 作者：【涂军】
/// 时间：2009-10-30
/// </summary>

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data;
using Hisuntech.SystemFramework.Exception;
//using Word = Microsoft.Office.Interop.Word;


namespace Hisuntech.SystemFramework.Common
{
    public class WordDotUtil
    {
        private static Word.Application WordApp;
        private static Object Nothing = System.Reflection.Missing.Value;
        private static Word.Document WordDoc;

        /// <summary>
        /// 打开word文档
        /// </summary>
        /// <param name="strFileName">word文档路径</param>
        /// <returns>返回一个Word.Document</returns>
        public static Word.Document OpenWordDot(string strFileName)
        {
            WordApp = new Word.ApplicationClass();
            WordApp.Visible = true;
            Word.Document WordDoc;
            Object filename = strFileName;
            Object ConfirmConversions = false;
            Object ReadOnly = false;
            Object AddToRecentFiles = false;
            Object PasswordDocument = System.Type.Missing;
            Object PasswordTemplate = System.Type.Missing;
            Object Revert = System.Type.Missing;
            Object WritePasswordDocument = System.Type.Missing;
            Object WritePasswordTemplate = System.Type.Missing;
            Object Format = System.Type.Missing;
            Object Encoding = System.Type.Missing;
            Object Visible = true;
            Object OpenAndRepair = System.Type.Missing;
            Object DocumentDirection = System.Type.Missing;
            Object NoEncodingDialog = System.Type.Missing;
            Object XMLTransform = System.Type.Missing;
            try
            {
                WordDoc = WordApp.Documents.Open(ref filename, ref ConfirmConversions,
                ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
                ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
                ref Encoding, ref Visible);
            }
            catch (System.Exception e)
            {
                return null;
            }
            return WordDoc;
        }
        /// <summary>
        /// 删除两个标签之间的内容
        /// </summary>
        /// <param name="doc">文档</param>
        /// <param name="strStartBookMark">起始标签</param>
        /// <param name="strEndMark">结束标签</param>
        /// <returns>0成功 -1 失败</returns>
        public static int WordDotDeleteParagraph(Word.Document doc, string strStartBookMark, string strEndMark)
        {
            Object oStartBookMark;
            Object oEndBookMark;
            Word.Bookmark start;
            Word.Bookmark end;
            Object oStart;
            Object oEnd;
            Object unit = Nothing;
            Object count = Nothing;
            try
            {
                if (doc.Bookmarks.Exists(strStartBookMark) && doc.Bookmarks.Exists(strEndMark))
                {
                    oStartBookMark = strStartBookMark;
                    oEndBookMark = strEndMark;

                    start = doc.Bookmarks.Item(ref oStartBookMark);
                    end = doc.Bookmarks.Item(ref oEndBookMark);

                    oStart = start.Start;
                    oEnd = end.Start;

                    doc.Range(ref oStart, ref oEnd).Select();
                    doc.Range(ref oStart, ref oEnd).Delete(ref unit, ref count);
                }
            }
            catch (System.Exception e)
            {
                return -1;
            }
            WordDotFieldsUpdate(doc);
            return 0;
        }

        /// <summary>
        /// 更新目录域
        /// </summary>
        /// <param name="doc">文档</param>
        /// <returns>-1 失败 0成功</returns>
        private static int WordDotFieldsUpdate(Word.Document doc)
        {
            try
            {
                doc.Content.Fields.Update();
            }
            catch (System.Exception e)
            {
                return -1;
            }
            return 0;
        }

        /// <summary>
        /// word文档保存
        /// </summary>
        /// <param name="doc">文档</param>
        /// <returns></returns>
        public static int SaveWord(Word.Document doc)
        {
            try
            {
                // 文件保存
                doc.Save();
            }
            catch (System.Exception e)
            {
                return -1;
            }
            return 0;
        }

        /// <summary>
        /// word文档另存为
        /// </summary>
        /// <param name="doc">文档</param>
        /// <param name="strSaveFileName">保存的文件名</param>
        /// <returns>-1失败 0正确</returns>
        public static int SaveAsWord(Word.Document doc, string strSaveFileName)
        {
            object objFalse = false;
            object objTrue = true;

            object wdRp = Word.WdReplace.wdReplaceAll;
            object wdSF = Word.WdSaveFormat.wdFormatDocument;
            object wdSO = Word.WdSaveOptions.wdSaveChanges;
            object wdOF = Word.WdOriginalFormat.wdWordDocument;
            //object wdLE = Word.WdLineEndingType.wdCRLF;
            //object wdEc = Microsoft.Office.Core.MsoEncoding.msoEncodingHZGBSimplifiedChinese;

            object oFileName = strSaveFileName;
            try
            {
                // 文件保存
                //doc.Save();
                doc.SaveAs(ref oFileName, ref wdSF, ref objFalse, ref Nothing, ref objTrue, ref Nothing, ref objFalse, ref objFalse, ref objFalse, ref objFalse, ref objFalse);

                //doc.SaveAs(ref  oFileName, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing, ref  Nothing);
            }
            catch (System.Exception e)
            {
                return -1;
            }
            return 0;
        }

        /// <summary>
        /// 关闭word文档
        /// </summary>
        /// <param name="doc">文档</param>
        /// <returns>-1失败，0正确</returns>
        public static int CloseWord(Word.Document doc)
        {
            try
            {
                if (doc != null)
                    doc.Close(ref  Nothing, ref  Nothing, ref  Nothing);
                if (WordApp != null)
                    WordApp.Quit(ref  Nothing, ref  Nothing, ref  Nothing);
            }
            catch (System.Exception e)
            {
                return -1;
            }
            return 0;
        }


        /// <summary>
        /// 关闭word文档
        /// </summary>
        /// <param name="doc">文档</param>
        /// <returns>-1失败，0正确</returns>
        public static int CloseWord()
        {
            try
            {
                if (WordDoc != null)
                    WordDoc.Close(ref  Nothing, ref  Nothing, ref  Nothing);
                if (WordApp != null)
                    WordApp.Quit(ref  Nothing, ref  Nothing, ref  Nothing);
            }
            catch (System.Exception e)
            {
                return -1;
            }
            return 0;
        }


        /// <summary>
        /// 往word文档里插数据
        /// </summary>
        /// <param name="doc">文档</param>
        /// <param name="strBookMark">标签</param>
        /// <param name="strInsertType">插入类型img 图片 text文本</param>
        /// <param name="strInsert">要插入的内容text的时候为文本内容，img的时候为图片路径</param>
        /// <returns>-1失败，0正确</returns>
        private static int WordDotBookMarkInsert(Word.Document doc, string strBookMark, string strInsertType, string strInsert)
        {
            object oBookMark = strBookMark;
            object Range = System.Reflection.Missing.Value;
            Object oLinkToFile = false;     //缺省  
            Object oSaveWithDocument = true;//缺省 
            try
            {
                foreach (Word.Bookmark BM in doc.Bookmarks)
                {
                    if (BM.Name.Equals(strBookMark,StringComparison.OrdinalIgnoreCase))
                    {
                        switch (strInsertType)
                        {
                            case "img":
                                BM.Select();
                                doc.Bookmarks.Item(ref oBookMark).Select();
                                Range = BM.Range;
                                Word.InlineShape ils = doc.InlineShapes.AddPicture(strInsert, ref oLinkToFile, ref oSaveWithDocument, ref Range);
                                break;
                            case "text":
                                BM.Select();
                                BM.Range.Text = strInsert;
                                break;
                        }

                        //Add break statement to exit loop by Tujun
                        break;
                    }
                }
            }
            catch (System.Exception e)
            {
                return -1;
            }
            return 0;
        }

        /// <summary>
        /// 插入表格
        /// </summary>
        /// <param name="doc">文档</param>
        /// <param name="strBookMark">书签</param>
        /// <param name="iRowNum">行数</param>
        /// <param name="iColNum">列数</param>
        /// <returns>返回一个table</returns>
        public static Word.Table WordDotBookMarkAddTable(Word.Document doc, string strBookMark, int iRowNum, int iColNum)
        {
            Word.Table newTable = null;
            foreach (Word.Bookmark BM in doc.Bookmarks)
            {
                //if (BM.Name.ToUpper() == strBookMark.ToUpper())
                if (BM.Name.Equals(strBookMark,StringComparison.OrdinalIgnoreCase))
                {
                    BM.Select();
                    newTable = doc.Tables.Add(BM.Range, iRowNum, iColNum, ref Nothing, ref Nothing);

                    //Add break statement to exit loop by Tujun
                    break;
                }
            }
            return newTable;
        }

        public static Word.Table StringConvertToTable(Word.Document doc, string strBookMark, string strConvert, int iRowNum, int iColNum)
        {
            Word.Table Table1 = null;
            for (int i = 0; i < doc.Bookmarks.Count; i++)
            {
                object obj = i + 1;
                Word.Bookmark BM = doc.Bookmarks.Item(ref obj);
                //if (BM.Name.ToUpper() == strBookMark.ToUpper())
                if (BM.Name.Equals(strBookMark,StringComparison.OrdinalIgnoreCase))
                {
                    object start = BM.Range.End;
                    object end = BM.Range.End + 1;
                    Word.Range range = doc.Range(ref start, ref end);
                    range.Select();
                    range.Text = strConvert;

                    object Separator = Word.WdTableFieldSeparator.wdSeparateByCommas;
                    object Format = Word.WdTableFormat.wdTableFormatNone;
                    object ApplyBorders = false;
                    object AutoFit = false;
                    object AutoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitContent;
                    object RowNumbers = iRowNum;
                    object CollumnNumbers = iColNum;

                    Table1 = range.ConvertToTable(ref Separator,
                        ref RowNumbers, ref CollumnNumbers, ref Nothing, ref Format,
                        ref ApplyBorders, ref Nothing, ref Nothing, ref Nothing,
                        ref Nothing, ref Nothing, ref Nothing,
                        ref Nothing, ref AutoFit, ref AutoFitBehavior,
                        ref Nothing);

                    //Add break statement to exit loop by Tujun
                    break;
                }
            }
            return Table1;
        }

        /// <summary>
        /// 设置行高
        /// </summary>
        /// <param name="table">表格</param>
        /// <param name="iRow">行</param>
        /// <param name="fHeight">高度</param>
        public static void SetTableRowHeight(Word.Table table, int iRow, float fHeight)
        {
            try
            {
                if (table != null)
                {
                    table.Rows.Item(iRow).HeightRule = Word.WdRowHeightRule.wdRowHeightExactly;
                    table.Rows.Item(iRow).Height = fHeight;
                }
            }
            catch (System.Exception ex)
            {
                ExceptionHandler.HandleException(ex);
            }
        }

        /// <summary>
        /// 设置表的所有行的行高
        /// </summary>
        /// <param name="table">表格</param>
        /// <param name="HeightRule">高度规格</param>
        /// <param name="fHeight">高度</param>
        public static void SetTableAllRowsHeight(Word.Table table, Word.WdRowHeightRule HeightRule, float fHeight)
        {
            table.Rows.HeightRule = HeightRule;
            table.Rows.Height = fHeight;
        }

        /// <summary>
        /// 设置列宽
        /// </summary>
        /// <param name="table">表格</param>
        /// <param name="iColumn">列</param>
        /// <param name="fWidth">列宽</param>
        public static void SetTableColumnWidth(Word.Table table, int iColumn, float fWidth)
        {
            try
            {
                if (table != null)
                {
                    table.Columns.Item(iColumn).Width = fWidth;
                }
            }
            catch (System.Exception ex)
            {
                ExceptionHandler.HandleException(ex);
            }
        }

        /// <summary>
        /// 设置表格行颜色
        /// </summary>
        /// <param name="table">表格</param>
        /// <param name="iRow">行</param>
        /// <param name="wdColor">颜色</param>
        public static void SetTableRowColor(Word.Table table, int iRow, Word.WdColor wdColor)
        {
            try
            {
                if (table != null)
                {
                    table.Rows.Item(iRow).Range.Shading.BackgroundPatternColor = wdColor;
                }
            }
            catch (System.Exception ex)
            {
                ExceptionHandler.HandleException(ex);
            }
        }

        /// <summary>
        /// 设置表格单元格颜色
        /// </summary>
        /// <param name="table">表格</param>
        /// <param name="iRow">行</param>
        /// <param name="iColumn">列</param>
        /// <param name="wdColor">颜色</param>
        public static void SetTableCellColor(Word.Table table, int iRow, int iColumn, Word.WdColor wdColor)
        {
            try
            {
                if (table != null)
                {
                    table.Cell(iRow, iColumn).Range.Shading.BackgroundPatternColor = wdColor;
                }
            }
            catch (System.Exception ex)
            {
                ExceptionHandler.HandleException(ex);
            }
        }

        public static void SetTableRowsFontColor(Word.Table table, int iRow, Word.WdColor wdColor)
        {
            try
            {
                if (table != null)
                {
                    table.Rows.Item(iRow).Range.Font.Color = wdColor;
                }
            }
            catch (System.Exception ex)
            {
                ExceptionHandler.HandleException(ex);
            }
        }
        /// <summary>
        /// 设置表格单元格字体颜色
        /// </summary>
        /// <param name="table">表格</param>
        /// <param name="iRow">行</param>
        /// <param name="iColumn">列</param>
        /// <param name="wdColor">颜色</param>
        public static void SetTableCellFontColor(Word.Table table, int iRow, int iColumn, Word.WdColor wdColor)
        {
            try
            {
                if (table != null)
                {
                    table.Cell(iRow, iColumn).Range.Font.Color = wdColor;
                }
            }
            catch (System.Exception ex)
            {
                ExceptionHandler.HandleException(ex);
            }
        }

        public static void SetTableRowsFontSize(Word.Table table, int iRow, float fSize)
        {
            try
            {
                if (table != null)
                {
                    table.Rows.Item(iRow).Range.Font.Size = fSize;
                }
            }
            catch (System.Exception ex)
            {
                ExceptionHandler.HandleException(ex);
            }
        }


        /// <summary>
        /// 设置单元格字体大小
        /// </summary>
        /// <param name="table">表格</param>
        /// <param name="iRow">行</param>
        /// <param name="iColumn">列</param>
        /// <param name="fSize">尺寸</param>
        public static void SetTableCellFontSize(Word.Table table, int iRow, int iColumn, float fSize)
        {
            try
            {
                if (table != null)
                {
                    table.Cell(iRow, iColumn).Range.Font.Size = fSize;
                }
            }
            catch (System.Exception ex)
            {
                ExceptionHandler.HandleException(ex);
            }
        }

        public static void SetTableFontName(Word.Table table, string strName)
        {
            try
            {
                if (table != null)
                {
                    table.Range.Font.Name = strName;
                }
            }
            catch (System.Exception ex)
            {
                ExceptionHandler.HandleException(ex);
            }
        }

        /// <summary>
        /// 设置表格字体大小
        /// </summary>
        /// <param name="table">表格</param>
        /// <param name="fSize">大小</param>
        public static void SetTableFontSize(Word.Table table, float fSize)
        {
            try
            {
                if (table != null)
                {
                    table.Range.Font.Size = fSize;
                }
            }
            catch (System.Exception ex)
            {
                ExceptionHandler.HandleException(ex);
            }
        }

        /// <summary>
        /// 设置表格单元格文本
        /// </summary>
        /// <param name="table">表格</param>
        /// <param name="iRow">行<列/param>
        /// <param name="iColumn"></param>
        /// <param name="strCellText">文本</param>
        public static void SetTableCellText(Word.Table table, int iRow, int iColumn, string strCellText)
        {
            try
            {
                if (table != null)
                {
                    table.Cell(iRow, iColumn).Range.Text = strCellText;
                }
            }
            catch (System.Exception ex)
            {
                ExceptionHandler.HandleException(ex);
            }
        }

        public static void SetTableRowsFontBold(Word.Table table, int iRow, int iBold)
        {
            try
            {
                if (table != null)
                {
                    table.Rows.Item(iRow).Range.Font.Bold = iBold;
                }
            }
            catch (System.Exception ex)
            {
                ExceptionHandler.HandleException(ex);
            }
        }

        /// <summary>
        /// 设置表格单元格字体粗度
        /// </summary>
        /// <param name="table">表格</param>
        /// <param name="iRow">行</param>
        /// <param name="iColumn">列</param>
        /// <param name="iBold">粗度</param>
        public static void SetTableCellFontBold(Word.Table table, int iRow, int iColumn, int iBold)
        {
            try
            {
                if (table != null)
                {
                    table.Cell(iRow, iColumn).Range.Bold = iBold;
                }
            }
            catch (System.Exception ex)
            {
                ExceptionHandler.HandleException(ex);
            }
        }

        /// <summary>
        /// 整体插入替换标签
        /// </summary>
        /// <param name="doc">文档</param>
        /// <param name="hTable">列表</param>
        /// <param name="cOptionFlag">分割字符</param>
        public static void WordDotHashTableInsert(Word.Document doc, Hashtable hTable, char cOptionFlag)
        {
            // Word.Document doc = OpenWordDot(filepath);

            foreach (DictionaryEntry de in hTable)
            {
                string strBookMark = de.Key.ToString();
                string strValues = de.Value.ToString();
                if (strValues.Length>0)
                {
                    string[] strArrValues = strValues.Split(cOptionFlag);
                    if (strArrValues.Length > 0)
                    {
                        int iResult = WordDotBookMarkInsert(doc, strBookMark, strArrValues[1].ToLower(), strArrValues[0].ToLower());
                    }
                }

            }

            //CloseWord(doc);
        }


        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="Path">文件路径</param>
        /// <param name="Strings">文件内容</param>
        public static void ZylWriteFile(string p_name, string p_str)
        {
            string p_path = Application.StartupPath + "\\" + p_name;
            if (!System.IO.File.Exists(p_path))
            {
                System.IO.FileStream f = System.IO.File.Create(p_path);
                f.Close();
            }
            System.IO.StreamWriter f2 = new System.IO.StreamWriter(p_path, true, System.Text.Encoding.GetEncoding("gb2312"));
            f2.WriteLine(p_str);
            f2.Close();
            f2.Dispose();
        }

        /// <summary>
        /// 跳转到指定的书签
        /// </summary>
        /// <param name="strBookMarkName">书签名</param>
        public static void GotoBookMark(string strBookMarkName)
        {
            object missing = System.Reflection.Missing.Value;

            object Bookmark = (int)Word.WdGoToItem.wdGoToBookmark;
            object NameBookMark = strBookMarkName;
            WordApp.Selection.GoTo(ref Bookmark, ref missing, ref missing, ref NameBookMark);
        }

        /// <summary>
        /// 插入段落
        /// </summary>
        public static void InsertParagraphBreak()
        {
            WordApp.Selection.TypeParagraph();
        }
        /// <summary>
        /// 插入段落
        /// </summary>
        /// <param name="nline">插入的段落的个数</param>
        public static void InsertParagraphBreak(int nline)
        {
            for (int i = 0; i < nline; i++)
                WordApp.Selection.TypeParagraph();
        }

        /// <summary>
        /// 向文档中加入图片
        /// </summary>
        /// <param name="path">物理路径</param>
        public static void InsertPicture(string strPath)
        {
            object linktofile = false;
            object savetofile = true;
            object missing = System.Reflection.Missing.Value;
            WordApp.Selection.InlineShapes.AddPicture(strPath, ref linktofile, ref savetofile, ref missing);
            WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
        }

        /// <summary>
        /// 为产品信息添加表
        /// </summary>
        /// <param name="name">产品名称</param>
        /// <param name="type">产品类型</param>
        /// <param name="liangdian">产品亮点</param>
        /// <param name="miaoshu">产品描述</param>
        public static void CreateTable(string name, string type, string liangdian, string miaoshu)
        {
            object missing = System.Reflection.Missing.Value;
            Word.Table newTable = WordApp.Selection.Tables.Add(WordApp.Selection.Range, 3, 4, ref missing, ref missing);
            newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
            newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
            //填充表格内容
            newTable.Cell(1, 1).Range.Text = "产品名称";
            newTable.Cell(1, 3).Range.Text = "产品类型";
            newTable.Cell(2, 1).Range.Text = "持有理由：";
            newTable.Cell(3, 1).Range.Text = "产品描述";
            newTable.Cell(1, 1).Range.Shading.BackgroundPatternColor = Word.WdColor.wdColorGray125;
            newTable.Cell(2, 1).Range.Shading.BackgroundPatternColor = Word.WdColor.wdColorGray125;
            newTable.Cell(3, 1).Range.Shading.BackgroundPatternColor = Word.WdColor.wdColorGray125;
            newTable.Cell(1, 3).Range.Shading.BackgroundPatternColor = Word.WdColor.wdColorGray125;
            newTable.Cell(1, 2).Range.Text = name;
            newTable.Cell(1, 4).Range.Text = type;
            newTable.Cell(2, 2).Range.Text = liangdian;
            newTable.Cell(3, 2).Range.Text = miaoshu;
            //合并单元格
            newTable.Cell(2, 2).Merge(newTable.Cell(2, 4));
            newTable.Cell(3, 2).Merge(newTable.Cell(3, 4));
            WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
        }

        /// <summary>
        /// 插入文字
        /// </summary>
        /// <param name="strText">要插入的文字居左</param>
        public static void InsertText(string strText)
        {
            WordApp.Selection.TypeText(strText);
            WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
        }

        /// <summary>
        /// 设置文字颜色
        /// </summary>
        /// <param name="strFontColor">颜色数值</param>
        public static void InsertText(string strText, string strFontColor)
        {
            switch (strFontColor.ToLower())
            {
                case "blue":
                    WordApp.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    break;
                case "gold":
                    WordApp.Selection.Font.Color = Word.WdColor.wdColorGold;
                    break;
                case "gray":
                    WordApp.Selection.Font.Color = Word.WdColor.wdColorGray875;
                    break;
                case "green":
                    WordApp.Selection.Font.Color = Word.WdColor.wdColorGreen;
                    break;
                case "lightblue":
                    WordApp.Selection.Font.Color = Word.WdColor.wdColorLightBlue;
                    break;
                case "orange":
                    WordApp.Selection.Font.Color = Word.WdColor.wdColorOrange;
                    break;
                case "pink":
                    WordApp.Selection.Font.Color = Word.WdColor.wdColorPink;
                    break;
                case "red":
                    WordApp.Selection.Font.Color = Word.WdColor.wdColorRed;
                    break;
                case "yellow":
                    WordApp.Selection.Font.Color = Word.WdColor.wdColorYellow;
                    break;
                case "black":
                    WordApp.Selection.Font.Color = Word.WdColor.wdColorBlack;
                    break;
            }
            WordApp.Selection.TypeText(strText);
            WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
        }

        public static void CreateJJMX(DataTable dt)
        {
            object missing = System.Reflection.Missing.Value;
            Word.Table newTable = WordApp.Selection.Tables.Add(WordApp.Selection.Range, dt.Rows.Count + 1, dt.Columns.Count, ref missing, ref missing);
            newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
            newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
            int n = 1;
            foreach (DataColumn ch in dt.Columns)
            {
                newTable.Cell(1, n).Range.Text = ch.Caption;
                n++;
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    newTable.Cell(i + 2, j + 1).Range.Text = dt.Rows[i][j].ToString();
                }
            }

            WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
        }
    }

}
